package com.witmed.lib_base

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.witmed.IView
import com.witmed.lib_common.utils.LoadDialog
import java.lang.reflect.ParameterizedType


abstract class BaseActivity<V:ViewDataBinding,VM:BaseViewModel<BaseModel>>:AppCompatActivity(),IView {

    lateinit var v:V
    lateinit var vm:VM
    lateinit var showDialogOb:Observer<String>
    lateinit var hiddenDialogOb:Observer<String>
    lateinit var startActivityOb:Observer<Map<String,Any>>
    lateinit var closeActivityOb:Observer<String>

    var observableList = mutableListOf<BaseLiveData<*>>()

    var observerList= mutableListOf<Observer<*>>()

    lateinit var dialog:LoadDialog

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        v=DataBindingUtil.setContentView<V>(this,initLayout())
        initVM()
        //创建ViewModel下livedata的观察者
        createLiveData()
        registerUIChange()
        initView()
        //给vm添加生命周期观察

        lifecycle.addObserver(vm)
    }



    private fun registerUIChange() {
        vm.getUiChangedListener()?.getStartActivityData()?.observeForever(startActivityOb)
        vm.getUiChangedListener()?.getCloseActivityData()?.observeForever(closeActivityOb)
        vm.getUiChangedListener()?.getShowDialogData()?.observeForever(showDialogOb)
        vm.getUiChangedListener()?.getHiddenDialogData()?.observeForever(hiddenDialogOb)
    }

    private fun createLiveData() {
        startActivityOb=object :Observer<Map<String,Any>>{
            override fun onChanged(value: Map<String, Any>) {
                startActivity()
            }
        }
        closeActivityOb=object :Observer<String>{
            override fun onChanged(value: String) {
                closeActivity()
            }
        }
        showDialogOb=object :Observer<String>{
            override fun onChanged(value: String) {
                showDialog()
            }
        }
        hiddenDialogOb=object :Observer<String>{
            override fun onChanged(value: String) {
                hiddenDialog()
            }
        }
    }

    fun showDialog(){
        dialog= LoadDialog(this)
        dialog.show()
    }

    fun hiddenDialog(){
        if(dialog!=null){
            dialog.dismiss()
        }
    }

    fun closeActivity(){
        finish()
    }


    abstract fun startActivity()

    override fun initVM() {
        vm=createVM()
        v.setVariable(initDB(),vm)
    }

    abstract fun createVM(): VM


    override abstract fun initDB():Int

    override fun onDestroy() {
        super.onDestroy()
        if(v!=null){
            v.unbind()
        }
        vm.getUiChangedListener()?.getStartActivityData()?.removeObserver(startActivityOb)
        vm.getUiChangedListener()?.getCloseActivityData()?.removeObserver(closeActivityOb)
        vm.getUiChangedListener()?.getShowDialogData()?.removeObserver(showDialogOb)
        vm.getUiChangedListener()?.getHiddenDialogData()?.removeObserver(hiddenDialogOb)
        lifecycle.removeObserver(vm)
    }
}